<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>nested 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-bucket-nested-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-nested-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-nested-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-bucket-nested-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-bucket-nested-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">桶(bucket)聚合</a></span>
»
<span class="breadcrumb-node">nested 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-missing-aggregation.html">« missing 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-parent-aggregation.html">parent 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-nested-aggregation"></a>嵌套(nested)聚合
</h2>
</div></div></div>
<p>一个特殊的单桶聚合，可以聚合嵌套(nested)文档。</p>
<p>例如，假设我们有一个产品索引，每个产品都有一个经销商列表——每个经销商都有自己的产品价格。映射(maping)可能是这样的：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /products
{
    "mappings": {
        "properties" : {
            "resellers" : { <a id="CO231-1"></a><i class="conum" data-value="1"></i>
                "type" : "nested",
                "properties" : {
                    "reseller" : { "type" : "text" },
                    "price" : { "type" : "double" }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/486.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO231-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">resellers</code>是保存嵌套文档的数组。</p>
</td>
</tr>
</table>
</div>
<p>下面这个请求添加了一个产品，这个产品有两个经销商：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /products/_doc/0
{
  "name": "LED TV", <a id="CO232-1"></a><i class="conum" data-value="1"></i>
  "resellers": [
    {
      "reseller": "companyA",
      "price": 350
    },
    {
      "reseller": "companyB",
      "price": 500
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/487.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO232-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>我们对<code class="literal">name</code>属性使用动态映射<span class="remark">(索引的映射中没有定义)</span>。</p>
</td>
</tr>
</table>
</div>
<p>下面这个请求返回产品的最低购买价格：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /products/_search
{
    "query" : {
        "match" : { "name" : "led tv" }
    },
    "aggs" : {
        "resellers" : {
            "nested" : {
                "path" : "resellers"
            },
            "aggs" : {
                "min_price" : { "min" : { "field" : "resellers.price" } }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/488.console"></div>
<p>
正如你在上面看到的，nested 聚合需要顶层文档中嵌套文档的路径（<code class="literal">path</code>）。

然后可以在这些嵌套文档上定义任何类型的聚合。
</p>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  ...
  "aggregations": {
    "resellers": {
      "doc_count": 2,
      "min_price": {
        "value": 350
      }
    }
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-missing-aggregation.html">« missing 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-parent-aggregation.html">parent 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>